﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Portal.Presentation.Controllers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Portal.Presentation.Providers;
using Portal.Logic.Services;
using Portal.Presentation.Converters;
using Portal.Presentation.Services;
using System.Web.Mvc;
using FluentAssertions;
using Portal.Presentation.Models;
using Rhino.Mocks;

namespace Portal.Presentation.Controllers.Tests.Units
{
	[TestClass()]
	public class UserInfoControllerTests : BaseControllerTests<UserInfoController>
	{
		private IUserInfoService _service;
		private IUserViewModelConverter _converter;

		protected override void InitializeTest()
		{
			base.InitializeTest();
			_converter = Mocks.DynamicMock<IUserViewModelConverter>();
			_service = Mocks.DynamicMock<IUserInfoService>();
		}

		#region Preview

		[TestMethod()]
		public void Preview_ModelIsNull_RedirectToError()
		{
			var controller = GetController();
			Mocks.ReplayAll();

			var actionResult = controller.Preview();
			var result = actionResult as RedirectToRouteResult;

			result.RouteValues["action"].Should().Be("General");
			result.RouteValues["controller"].Should().Be("Error");
		}

		[TestMethod()]
		public void Preview_ModelIsValid_ReturnsView()
		{
			var model = new UserInfoDataModel();
			Expect.Call(_converter.ToModel(null)).IgnoreArguments().Return(model);

			var controller = GetController();
			Mocks.ReplayAll();

			var actionResult = controller.Preview();
			var result = actionResult as ViewResult;

			result.Model.Should().BeOfType<UserInfoDataModel>();
		}

		#endregion

		#region POST: Edit

		[TestMethod()]
		public void POST_Edit_ModelIsValid_RedirectToIndex()
		{
			var model = new UserInfoDataModel();

			var controller = GetController();
			Mocks.ReplayAll();

			var actionResult = controller.Edit(model);
			var result = actionResult as RedirectToRouteResult;

			result.RouteValues["action"].Should().Be("Preview");
		}

		[TestMethod()]
		public void POST_Edit_ModelIsNotValid_ReturnsView()
		{
			var model = new UserInfoDataModel();
			var controller = GetController();
			controller.ModelState.AddModelError("key", "error message");
			Mocks.ReplayAll();

			var actionResult = controller.Edit(model);
			var result = actionResult as ViewResult;

			result.Model.Should().BeNull();
		}

		[TestMethod()]
		public void POST_Edit_ExceptionIsThrown_ReturnsView()
		{
			var model = new UserInfoDataModel();
			var controller = GetController();
			Expect.Call(SecurityProvider.GetUser(null)).IgnoreArguments().Throw(new Exception());
			Mocks.ReplayAll();

			var actionResult = controller.Edit(model);
			var result = actionResult as ViewResult;

			result.Model.Should().BeNull();
		}
		#endregion

		protected override UserInfoController GetController()
		{
			return new UserInfoController(SecurityProvider, _service, SessionService, _converter);

		}
	}
}
